import sys
import cv

class videoInput:
	camera = 0
	filename = ""
	capture = None

	def __init__(self, ident):
		if type(ident) == int:
			self.camera = ident
			self.capture = cv.CaptureFromCAM(self.camera)
		elif type(ident) == str:
			self.filename = ident
			self.capture = cv.CaptureFromFile(self.filename)
		else:
			print "Could not open $1 for capture....exiting!" % (ident)
			sys.exit(1)

		if not self.capture:
			print "Could not open $1 for capture....exiting!" % (ident)
			sys.exit(1)

	def __del__(self):
		pass
		#cv.ReleaseCapture(self.capture)

	@property
	def width(self):
		return cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_WIDTH)

	@property
	def height(self):
		return cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_HEIGHT)

	@property
	def fps(self):
		return cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_FPS)

	@property
	def frameSize(self): 
		return (int(self.width()), int(self.height()))

	@property
	def frame(self):
		return cv.QueryFrame(self.capture)

if __name__ == '__main__':
	cv.NamedWindow("camera", 1)
	video = videoInput("../sample-videos/os6sense-sample-1-opencv-cap-1280x720.avi")

	while True:
		img = video.frame
		#print video.fps
		cv.ShowImage("camera", img)
		if cv.WaitKey(50) == 27:
			break

